package com.example.growthmuseum.goods;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.growthmuseum.R;
import com.example.growthmuseum.ServiceFactory;
import com.example.growthmuseum.data.model.Goods;
import com.example.growthmuseum.data.model.Result;
import com.example.growthmuseum.goodsdesc.GoodsDescActivity;
import com.example.growthmuseum.postgoods.PostGoodsActivity;
import com.example.growthmuseum.service.GoodsService;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.orhanobut.logger.Logger;

import java.util.List;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * Created by Jacob on 2016/10/29.
 */

public class GoodsFragment extends Fragment
        implements SwipeRefreshLayout.OnRefreshListener{

    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    private GoodsAdapter goodsAdapter;

    private EasyRecyclerView recyclerView;

    private FloatingActionButton postFab;

    private GoodsService goodsService;

    public GoodsFragment() {
    }

    public static GoodsFragment newInstance() {
        return new GoodsFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {
       return inflater.inflate(R.layout.fragment_goods, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        super.onViewCreated(view, savedInstanceState);
        goodsAdapter = new GoodsAdapter(getActivity());
        this.goodsService = ServiceFactory
                .createRetrofitService(GoodsService.class,
                        ServiceFactory.BASE_URL);
        initViews(view);
        onRefresh();
    }

    private void initViews(View view) {
        recyclerView = (EasyRecyclerView) view.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapterWithProgress(goodsAdapter);
        recyclerView.setRefreshListener(this);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        goodsAdapter.setNoMore(R.layout.view_no_more);
        goodsAdapter.setOnItemClickListener(
                this::toGoodsActivity);

        postFab = (FloatingActionButton) view.findViewById(R.id.post_fab);
        postFab.setOnClickListener(view1 -> {
            Intent toPost = new Intent(getActivity(), PostGoodsActivity.class);
            startActivity(toPost);
        });
    }
    private void toGoodsActivity(int position) {
        Intent toGoodsDesc = new Intent(getActivity(), GoodsDescActivity.class);
        toGoodsDesc.putExtra("id", goodsAdapter.getItem(position).getId());
        startActivity(toGoodsDesc);
    }

    @Override
    public void onRefresh() {
        goodsService.getGoodsList()
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Result<List<Goods>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Logger.e(e, e.getMessage());

                    }

                    @Override
                    public void onNext(Result<List<Goods>> result) {
                        if (result.getCode().equals(Result.SUCCESS)) {
                            setGoodsList(result.getBean());
                        } else {
                            Toast.makeText(getActivity(),
                                    result.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }
    private void setGoodsList(List<Goods> goodsList) {
        goodsAdapter.clear();
        goodsAdapter.addAll(goodsList);
        goodsAdapter.notifyDataSetChanged();
    }
}
